package com.cisetech.service;


import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClientDemo {
 public String sendMessage(String reqXml,String urlpath) throws Exception{
	  HttpURLConnection conn = null;
	  String response=" ";
      
          // 访问地址
          URL url = new URL(urlpath);

          // 打开连接及基本属性设置
          conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
          conn.setDoInput(true);
          conn.setDoOutput(true);
          // 写入请求报文
          OutputStreamWriter writer = new OutputStreamWriter(
                  conn.getOutputStream(), "UTF-8");
          IOUtils.write(reqXml, writer);
          writer.close();

          // 读取响应报文
          InputStream in = conn.getInputStream();
          System.out.println("------ Response ------");
          response = IOUtils
                  .toString(new InputStreamReader(in, "UTF-8"));
        
      
          if (conn != null) {
              conn.disconnect();
          }
      
	return response;
  }

	 
 }
 
